package todlu.RssReader.QQWeibo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

/**
 *@author coolszy
 *@date 2011-6-13
 *@blog http://blog.csdn.net/coolszy
 */

/**
 * 杈呭姪绫�
 */
public class TextUtil
{
	/**
	 * 鍒ゆ柇瀛楃涓叉槸鍚︿负绌�
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str)
	{
		if (null==str||str.equals(""))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 鍒嗗壊杩斿洖缁撴灉
	 * @param response 杩斿洖瀛楃涓�
	 * @return
	 */
	public static Map<String, String> splitResponse(String response) throws Exception
	{
		//淇濆瓨杩斿洖缁撴灉
		Map<String, String> map = new HashMap<String, String>();
		//鍒ゆ柇鏄惁涓虹┖
		if (!TextUtil.isEmpty(response))
		{
			//宸测�&鈥濊繘琛屽垎鍓�
			String[] array = response.split("&");
			if (array.length > 2) 
			{
				String tokenStr = array[0]; //oauth_token=xxxxx
				String secretStr = array[1];//oauth_token_secret=xxxxxxx
				String[] token = tokenStr.split("=");
				if (token.length == 2) 
				{
					map.put("oauth_token", token[1]);
				}
				String[] secret = secretStr.split("=");
				if (secret.length == 2) 
				{
					map.put("oauth_token_secret", secret[1]);
				}
			}
			else
			{
				throw new Exception("error");
			}
		}
		else
		{
			throw new Exception("error");
		}
		return map;
	}
	
	/**
	 * 鍙傛暟鍙嶇紪鐮�
	 * 
	 * @param s
	 * @return
	 */
	public static String decode(String s)
	{
		if (s == null)
		{
			return "";
		}
		try
		{
			return URLDecoder.decode(s, "UTF-8");
		} catch (UnsupportedEncodingException e)
		{
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
}
